//
//  TLGifAniButton.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/10/14.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//

#import "TLGifAniButton.h"


@interface TLGifAniButton () 
@property (nonatomic,strong) UIImageView *aniIV;

@end

@implementation TLGifAniButton
@synthesize aniIV,on;

- (id)initWithCoder:(nonnull NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    
    if(self)
    {
        [self addTarget:self action:@selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        aniIV = [[UIImageView alloc] initWithFrame:self.frame];
        
        [aniIV setBackgroundColor:[UIColor clearColor]];
        
        self.triggerOnTouch = YES;
    }
    
    return self;
}

- (void)awakeFromNib{
    //animation image view
    [self installAnimationLayer];
}

- (void)installAnimationLayer{
    if(!self.normalImgName)
        self.normalImg = [self backgroundImageForState:UIControlStateNormal];
    else
        self.normalImg = [UIImage imageNamed:self.normalImgName];
    
    if(!self.highlightImgName)
        NSLog(@"Highlighted image 不存在");
    
    self.highlightImg = [UIImage imageNamed:self.highlightImgName];
    
    if(!self.highlightImgName)
        NSLog(@"Mask image 不存在");
    
    
    
    self.aniMask = [UIImage imageNamed:self.aniMaskName];
    
    [aniIV setImage:self.aniMask];
    
    self.on = NO;
}

- (void)invoke{
    
    
    [self invokeAnimation];
}

- (void)invokeAnimation{
    [self addKeyFrameTo:aniIV andAnimationType:TLAniBtnAniTypeSlantMove];
}

- (void)touchUpInside{
    [self setOn:!self.on];
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    if(self.triggerOnTouch)
    {
        [self invoke];
    }
}

- (void)addKeyFrameTo:(UIView *)view andAnimationType:(TLAniBtnType)aniType{
    switch (aniType) {
        case TLAniBtnAniTypeSlantMove:
            [self animationSlantMoveAddKeyFrameTo:view];
            break;
            
        default:
            break;
    }
}

- (void)animationSlantMoveAddKeyFrameTo:(UIView *)view{
    [self.superview addSubview:view];
    
    CAKeyframeAnimation *aniY = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
    aniY.keyTimes = @[@(0.0),@(0.4),@(0.6),@(1.0),@(1.2)];
    
    aniY.values = @[@(-10),@(-15),@(-20),@(-25),@(-30)];
    
    CAKeyframeAnimation *aniX = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    aniX.keyTimes = @[@(0.0),@(0.4),@(0.6),@(1.0),@(1.2)];
    
    aniX.values = @[@(10),@(15),@(20),@(25),@(30)];
    
    CAKeyframeAnimation *alphaCg = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    alphaCg.keyTimes = @[@(0.0),@(0.4),@(0.6),@(1.0),@(1.2)];
    alphaCg.values = @[@(1.0),@(0.8),@(0.6),@(0.2),@(0.0)];
    
    aniX.calculationMode = kCAAnimationLinear;
    aniY.calculationMode = kCAAnimationLinear;
    alphaCg.calculationMode = kCAAnimationLinear;
    //[view.layer addAnimation:aniY forKey:@"SlantMove"];
    //[view.layer addAnimation:aniX forKey:@"SlantMove"];
    
    CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
    
    aniGroup.delegate = self;
    
    aniGroup.animations = @[aniY,aniX,alphaCg];
    
    view.layer.anchorPoint = CGPointMake(0.5, 0.5);
    
    [view.layer addAnimation:aniGroup forKey:@"SlantMove"];
}

- (void)animationDidStart:(nonnull CAAnimation *)anim{
        if(self.on)
            [self setBackgroundImage:self.highlightImg forState:UIControlStateNormal];
        else
            [self setBackgroundImage:self.normalImg forState:UIControlStateNormal];

}

- (void)animationDidStop:(nonnull CAAnimation *)anim finished:(BOOL)flag{
    if(flag)
    {
        [self.aniIV removeFromSuperview];
    }
}


#pragma mark -- Setter&Getter

- (void)setOn:(BOOL)on_{
    on ? [self setBackgroundImage:self.highlightImg forState:UIControlStateNormal]:[self setBackgroundImage:self.normalImg forState:UIControlStateNormal];
    
    on = on_;
}

- (void)select{
    
    if(![self on])
    {
        [self setOn:YES];
    [self addKeyFrameTo:aniIV andAnimationType:TLAniBtnAniTypeSlantMove];
    }
    
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}
- (void)unselect{
    
    
    if([self on])
    {
    [self setOn:NO];
    [self addKeyFrameTo:aniIV andAnimationType:TLAniBtnAniTypeSlantMove];
    }
    
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

@end
